//
//  ACIAPManager.h
//  ACFramework
//
//  Created by caoyq on 2018/6/11.
//  Updated by caoyq on 2018/6/20.
//

#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@class ACIAPManager;

static NSInteger ACIAPErrorCode0 = 2000;
static NSInteger ACIAPErrorCode1 = 2001;
static NSInteger ACIAPErrorCode2 = 2002;
static NSInteger ACIAPErrorCode3 = 2003;
static NSInteger ACIAPErrorCode4 = 2004;
static NSInteger ACIAPErrorCode5 = 2005;
static NSInteger ACIAPErrorCode6 = 2006;
static NSString *const ACIAPErrorCode0Description = @"该机器不支持苹果支付";
static NSString *const ACIAPErrorCode1Description = @"提供的productId不能为空";
static NSString *const ACIAPErrorCode2Description = @"已售商品列表中没有商品，请去商品后台新建";
static NSString *const ACIAPErrorCode3Description = @"已售商品列表中找不到对应的productId商品";
static NSString *const ACIAPErrorCode4Description = @"购买失败";
static NSString *const ACIAPErrorCode5Description = @"用户取消交易";
static NSString *const ACIAPErrorCode6Description = @"找不到用于校验的票据";
static NSString *const ACIAPErrorCode7Description = @"苹果返回的校验票据失败，具体原因查看错误码";

@protocol ACIAPManagerDelegate<NSObject>

@required

///支付失败代理方法
- (void)ac_iapManager: (ACIAPManager *)ipaManager paymentDidFailWithError: (NSError *)error;

///支付成功的方法
- (void)ac_iapManagerPaymentDidSuccess: (ACIAPManager *)ipaManager;

@end

/**
  in-App Purchase. 苹果支付方法的总结封装
 */
@interface ACIAPManager : NSObject

///初始化单例
+ (instancetype)manager;

/**
 添加监听者，并设置代理.
 
 一个控制器中仅需添加一次，用于 ACIAPManager 对支付反馈的监听.

 @param delegate ACIAPManagerDelegate代理
 */
- (void)addObserverAndDelegate: (id<ACIAPManagerDelegate>)delegate;

/**
 移除监听者.
 
 一个控制器中只需要在dealloc方法中调用即可.
 */
- (void)removeObserver;

/**
 开始支付.
 
 每购买一个商品调用一次，一次购买一个商品.

 @param productId 购买的商品的id
 @param isSandBoxEnvironment 是否是在沙盒环境测试.
 */
- (void)startPaymentWithID: (NSString *)productId isSandBoxEnvironment: (BOOL)isSandBoxEnvironment;

@end
